import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: "/",
    name: "Index",
    redirect: '/home',  // 重定向:重新指向其它path,会改变网址
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    redirect: "/home/homePage",
    children: [
      {
        path: "homePage",
        name: "HomePage",
        meta: {
          title: "首页"
        },
        component: () => import('../views/home/HomePage.vue')
      },
      {
        path: "allJob",
        name: "AllJob",
        meta: {
          title: "所有兼职"
        },
        component: () => import('../views/home/AllJob.vue')
      },
      {
        path: "squer",
        name: "Squer",
        meta: {
          title: "广场"
        },
        component: () => import('../views/home/Squer.vue')
      },
      {
        path: "message",
        name: "Message",
        meta: {
          title: "消息"
        },
        component: () => import('../views/home/Message.vue')
      },
      {
        path: "my",
        name: "My",
        meta: {
          title: "我的",
          requiresAuth: true
        },
        component: () => import('../views/home/My.vue')
      },
    ]
  },
  {
    path: '/hope',
    name: 'Hope',
    meta: {
      title: "求职意向"
    },
    component: () => import('../views/Hope.vue')
  },
  {
    path: '/login',
    name: 'Login',
    meta: {
      title: "登录"
    },
    component: () => import('../views/Login.vue')
  },
  {
    path: '/set',
    name: 'Set',
    meta: {
      title: "设置"
    },
    component: () => import('../views/Set.vue')
  }
]
// 创建路由实例
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


//获取路由上的原型方法
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

// 前置守卫(检查是否登录吗，如果没有登录，则跳转到登录页面)
router.beforeEach((to, from, next) => {

  if (to.meta.requiresAuth && !localStorage.getItem("token")) {
    next("/login")
  }
  next()
})




export default router
